//
//  FYNetworkingRequestUnit.m
//  FYNetworking
//
//  Created by Felix Yin on 2017/5/5.
//  Copyright © 2017年 Felix Yin. All rights reserved.
//

#import "FYNetworkingRequestUnit.h"
#import "FYNetworkingManager.h"
#import "NSObject+FYNetworkingDeallocClearnRequest.h"

@interface FYNetworkingRequestUnit()

@property (nonatomic, strong) NSNumber *requestID;

@end

@implementation FYNetworkingRequestUnit

+(FYNetworkingRequestUnit *)request:(NSObject *) sender  requestParam:(NSDictionary *)requestParam APIPath:(NSString *)apiPath requestType:(FYREquestType)requestType resultProgressCallBack:(FYRequestProgressCallBack)progressCallBack operationCallBack:(FYRequestOperationCallBack)operationCallBack completeCallBack:(FYRequestCompleteCallBack)completeCallBack{
    __weak typeof(sender) weakSender = sender;
    FYNetworkingRequestUnit *requestUnit = [[FYNetworkingRequestUnit alloc] init];
    FYNetworkingFileRequestStting *requestSetting = [[FYNetworkingFileRequestStting alloc] initWithRequestPath:apiPath requestParameter:requestParam requestType:requestType requestProgressCallBack:progressCallBack requestOperationCallBack:operationCallBack requestCompleteCallBack:^(id data, NSError *requestError) {
        if(completeCallBack){
            completeCallBack(data,requestError);
        }
        //移除请求
        [weakSender.deallocClearnRequest cleanRequestUnitWithRequestID:requestUnit.requestID];
    }];
    // 请求配置完成，发起网络请求
    NSNumber *requestID = [[FYNetworkingManager shareInstance] sendRequest:requestSetting];
    //保存requestID
    requestUnit.requestID = requestID;
    [weakSender.deallocClearnRequest cacheRequestUnit:requestUnit requestID:requestUnit.requestID];
    
    return requestUnit;
}

+(FYNetworkingRequestUnit *)request:(NSObject *)sender requestParam:(NSDictionary *)requestParam APIPath:(NSString *)apiPath uploadFileParameterName:(NSString *)uploadFileParameterName fileName:(NSString *)fileName filePath:(NSString *)filePath mineType:(NSString *)mineType requestType:(FYREquestType)requestType resultProgressCallBack:(FYRequestProgressCallBack)progressCallBack uploadProgressCallBack:(FYRequestProgressCallBack)uploadProgressCallBack operationCallBack:(FYRequestOperationCallBack)operationCallBack completeCallBack:(FYRequestCompleteCallBack)completeCallBack{
    
    FYNetworkingRequestUnit *requestUnit = [[FYNetworkingRequestUnit alloc] init];
    __weak typeof(sender) weakSender = sender;
    FYNetworkingFileRequestStting *setting = [[FYNetworkingFileRequestStting alloc] initWithFileRequestPath:apiPath requestParameter:requestParam requestType:requestType fileName:fileName dataName:uploadFileParameterName filePath:filePath mineType:mineType uploadProgressCallBack:uploadProgressCallBack downloadProgress:nil requestOperationCallBack:operationCallBack requestCompleteCallBack:^(id data, NSError *requestError) {
        if(completeCallBack){
            completeCallBack(data,requestError);
        }
        [weakSender.deallocClearnRequest cleanRequestUnitWithRequestID:requestUnit.requestID];
    }];
    
    NSNumber *requestID = [[FYNetworkingManager shareInstance] sendRequest:setting];
    requestUnit.requestID = requestID;
    [weakSender.deallocClearnRequest cacheRequestUnit:requestUnit requestID:requestUnit.requestID];
    return requestUnit;
}

- (void)cancleRequest{
    if(self.requestID){
        [[FYNetworkingManager shareInstance] cancleRequest:self.requestID];
    }
}

- (void)dealloc
{
    [self cancleRequest];
    NSLog(@"RequestUnit Dealloc");
}

@end
